<html>
<head>
	<script type="text/javascript">
		var aJaxURL			= "server-side/view/recipie.action.php";				//server side folder url
		var list_aJaxURL	= "server-side/view/recipie/list.action.php";			//server side folder url
		var seoyURL			= "server-side/seoy/seoy.action.php";					//server side folder url
		var upJaxURL		= "server-side/upload/file.action.php";
		var tName			= "example";											//table name
		var fName			= "add-edit-form";										//form name
		var id				= "";
		
		$(document).ready(function () {
			//ClearDB();
			
			LoadTable(tName);
						
			/* Add Button ID, Delete Button ID */
			GetButtons("add_button", "delete_button");
			
			SetEvents("add_button", "delete_button", "check-all", tName, fName, aJaxURL);
		});
		
        //SeoYyy
		$(document.body).click(function (e) {
        	$("#rec_production").autocomplete("close");
        });
	
		function LoadTable(table){

			var local_id	= $("#local_rec_id").val();
					
			if(table == "prod_list"){
				var menuLength	= [[15], [15]];
				
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
				GetDataTable("prod_list", list_aJaxURL, "get_list", 5, "local_id=" + local_id, 0, menuLength, 1, "asc");
			}else{
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
                GetDataTable(tName, aJaxURL, "get_list", 3, "", 0, "", 0, "desc");
			}
		}


		
		function LoadDialog(form){
			var id			= $("#rec_id").val();
			
			$("#choose_button").button({
	            icons: {
	                primary: "ui-icon-arrowreturnthick-1-n"
	            }
        	});

			$("#upload_button").button({
	            icons: {
	                primary: "ui-icon-arrowreturnthick-1-n"
	            }
        	});

           
			var img_url	= $("#upload_img").attr("src");
	    	img_name	= img_url.split("\/")[4]; //Get image name element 4
	    	if(img_name != "0.jpg"){
	    		$("#choose_button").button("disable");
	    	}
	    	
		
			if(form == fName){
				
				//ClearDB();
				var local_id = GetLocalID();
				
				/* Dialog Form Selector Name, Buttons Array */
				GetDialog(fName, 680, "auto", "");
				
				
				if(empty(id)){			
					$("#local_rec_id").val(local_id);	
				}else{
					$("#local_rec_id").val(id);
				}
							
				GetButtons("add_button_prod", "delete_button_prod");
				LoadTable("prod_list");
				SetEvents("add_button_prod", "delete_button_prod", "check-all-prod", "prod_list", "add-edit-prod-form", list_aJaxURL);	
			}else{	
				var pr_name=$("#rec_production").val();
				if(pr_name != ""){
					 $("#combo").prop("disabled",true);
					 $("#rec_production").prop("disabled",true);					
				}		
				SeoY("rec_production", seoyURL, "production_name", "", 0);
				var buttons = {
			        "save": {
			            text: "დამატება",
			            id: "add-production",
			            click: function () {
			            	
			            }
			        }
			    };
				GetDialog("add-edit-prod-form", 500, 300, buttons);
			}
		}

		$(document).on("click","#add_button", function () {
			var id = GetLocalID();
		});

		
		
		function CloseDialog(form){
			$("#" + form).dialog("destroy");
		}


		
	    // Add - Save
	    $(document).on("click", "#save-dialog", function () {

            var rec_id	= $("#rec_id").val();
            var l_id	= $("#local_rec_id").val();
        	var check	= CheckProdList(l_id);
        	
        	if(check){
		    	param = new Object();
		    	
	            //Action
		    	param.act			= "save_rec";		    

			    param.code         	= $("#code").val();
			    param.name 			= $("#name").val();
			    param.cat			= $("#category").val();
			    param.un			= $("#unit").val();
			    param.l_id	    	= $("#local_rec_id").val();
			    param.rec_id		= $("#rec_id").val();

			    

				//Image
		    	param.img 	= img_name;
			    			    
			    	    	
		    	
		    	if(param.name == ""){
					alert("შეავსეთ რეცეპტის სახელი!");
				} else if(param.code == ""){
					alert("შეავსეთ კოდი");
				} else if(param.cat == ''){
					alert("შავსეთ კატეგორია");
				}else if(param.un == ""){
					alert("შეავსეთ ზომის ერთეული");
				}
					else {
				    $.ajax({
				        url: aJaxURL,
					    data: param,
				        success: function(data) {
							if(typeof(data.error) != "undefined")
							{
								if(data.error != "")
								{
									alert(data.error);
								}else
								{
									LoadTable(fName);
					        		CloseDialog(fName);
								}
							}
					    }
				    });
				}
			}
		});
		
	    // Add - Save Production
	    $(document).on("click", "#add-production", function () {
	    	param = new Object();
	    	
            //Action
	    	param.act		= "save_prod";		    
		    
		    param.id		= $("#production_id").val();	    	
		    param.l_id		= $("#local_rec_id").val();
		    param.rn		= $("#rec_production").val();
		    param.am        = $("#default_amount").val();
		    param.qn        = $("#rec_quantity").val();
		    param.pr        = $("#rec_price").val();

		    if($('#checkbox').is(':checked')){
	    		param.ch	= 1;
	    	}else{
	    		param.ch	= 0;
		    }
	    	
	    	if(param.rn == ""){
				alert("შეავსეთ პროდუქტის სახელი!");
			}else if(param.qn == ""){
				alert("შეავსეთ პროდუქტის რაოდენობა!");
			}else {
			    $.ajax({
			        url: list_aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined")
						{
							if(data.error != "")
							{
								alert(data.error);
							}else
							{
								LoadTable("prod_list");
								$("#add-edit-prod-form").dialog().dialog("destroy");								
							}
						}
				    }
			    });
			}
		});
		
        $(document).on("click", ".combobox", function (event) {
	    	var i = $(this).text();
			$("#" + i).autocomplete( "search", "" );
		});
		
        function CheckProdList(rec_id) {
            var check = false;
            $.ajax({
                url: aJaxURL,
                async: false, //r-value
                data: "act=check_prod_list&id=" + rec_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            check = true;
                        }
                    }
            	}
        	});
            return check;
        }
		
		/**
		* Get next auto incremented id
		* @return {int}   Returns a next id
		*/
		function GetLocalID(){
			var local_id;
			$.ajax({
		        url: aJaxURL,
        		async: false, //r-value
			    data: "act=get_local_id",
		        success: function(data) {
					if(typeof(data.error) != "undefined"){
						if(data.error != ""){
							alert(data.error);
						}else{
							local_id = data.local_id;
						}
					}
			    }
		    });
	      	return local_id;
		}
		
        
	// Add Image
	    $(document).on("click", "#choose_button", function () {
		    $("#choose_file").click();
		});

	    $(document).on("change", "#choose_file", function () {
	    	var file		= $(this).val();	    
		    var name		= uniqid();
		    var path		= "../../media/uploads/images/production/";
		    
		    var ext = file.split('.').pop().toLowerCase();
	        if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) { //echeck file type
	        	alert('This is not an allowed file type.');
                this.value = '';
	        }else{
	        	img_name = name + "." + ext;
	        	$("#choose_button").button("disable");
	        	$.ajaxFileUpload({
	    			url: upJaxURL,
	    			secureuri: false,
	    			fileElementId: "choose_file",
	    			dataType: 'json',
	    			data:{
						act: "upload_file",
						path: path,
						file_name: name,
						type: ext
					},
	    			success: function (data, status){
	    				if(typeof(data.error) != 'undefined'){
    						if(data.error != ''){
    							alert(data.error);
    						}else{
    							$("#upload_img").attr("src", "media/uploads/images/production/" + img_name);
    						}
    					}
    				},
    				error: function (data, status, e)
    				{
    					alert(e);
    				}
    			});
	        }
		});
		
	    $(document).on("click", "#view_image", function () {
		    var src = $("#upload_img").attr("src");
		    $("#view_img").attr("src", src);
			var buttons = {
				"cancel": {
		            text: "დახურვა",
		            id: "cancel-dialog",
		            click: function () {
		                $(this).dialog("close");
		            }
		        }
		    };
	    	GetDialog("image-form", "auto", "auto", buttons);
		});
		
	    $(document).on("click", "#upload_img", function () {
		    var src = $("#upload_img").attr("src");
		    $("#view_img").attr("src", src);
			var buttons = {
				"cancel": {
		            text: "დახურვა",
		            id: "cancel-dialog",
		            click: function () {
		                $(this).dialog("close");
		            }
		        }
		    };
	    	GetDialog("image-form", "auto", "auto", buttons);
		});
		
	    $(document).on("click", "#delete_image", function () {
	    	var img_url	= $("#upload_img").attr("src");
	    	img_name	= img_url.split("\/")[4];	//Get image name element 4
	    	if(img_name != "0.jpg"){
		    	param = new Object();
		    	
	            //Action
		    	param.act		= "delete_file";
		    	
		    	param.path	 	= "../../media/uploads/images/production/";
			    param.file_name	= img_name;
			    var id			= $("#cat_id").val();
			    
	            $.ajax({
	                url: upJaxURL,
	                data: param,
	                success: function(data) {
	                    if (typeof(data.error) != "undefined") {
	                        if (data.error != "") {
	                            alert(data.error);
	                        } else {
	                        	$("#choose_button").button("enable");
	                        	$("#upload_img").attr("src", "media/uploads/images/production/0.jpg");
	                        	if(!empty(id)){
	                        		DeleteImage(id);
		                        }
	                        }
	                    }
	                }
	            });
			}
		});
		
	    function DeleteImage(cat_id) {
            $.ajax({
                url: aJaxURL,
                data: "act=delete_image&id=" + cat_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                    	if (data.error != "") {
                            alert(data.error);
                        } else{
                        	img_name = "0.jpg";                        	
                        }
                    }
                }
            });
        }

        function ClearDB() {
            $.ajax({
                url: aJaxURL,
                data: "act=clear_db",
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        }
                    }
                }
            });
        }

		$(document).on("keyup", "#code, #rec_quantity, #rec_price, #default_amount", function() {
		    var val = $(this).val();
		    if(isNaN(val)){
		    	alert("მოცემულ ველში შეიყვანეთ მხოლოდ ციფრები");
		         val = val.replace(/[^0-9\.]/g,'');
		         if(val.split('.').length>2) 
		             val =val.replace(/\.+$/,"");
		    }
		    $(this).val(val); 
		});
	    
    </script>
</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            <div id="dynamic">
            	<h2 align="center">რეცეპტები</h2>
	        	<div id="button_area">
	        		<button id="add_button">დამატება</button><button id="delete_button">წაშლა</button>
	        	</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th style="width: 700px">რეცეპტის სახელი</th>
                            <th style="width: 100px">თვითღირებულება</th>                           
                            <th class="check">#</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="id" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="recipie_name" value="ფილტრი" class="search_init" />
                            </th>  
                            <th>
                                <input type="text" name="recipie_name" value="ფილტრი" class="search_init" />
                            </th>                        
                            <th>
                            	<input type="checkbox" name="check-all" id="check-all">
                            </th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>
    
    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="რეცეპტები">
    	<!-- aJax -->
	</div>
	
	    <div id="add-edit-prod-form" class="form-dialog" title="რეცეპტები">
    	<!-- aJax -->
	</div>	
	    <!-- jQuery Dialog -->
    <div id="image-form" class="form-dialog" title="პროდუქციის სურათი">
    	<img id="view_img" src="media/uploads/images/production/0.jpg">
	</div>	
</body>
</html>